package com.orangelife.service.propFeeMeter.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.feelog.IFeeLogService;
import com.orangelife.service.feelog.modal.PropFeeLogDto;
import com.orangelife.service.propFeeMeter.IPropFeeMeterService;
import com.orangelife.service.propFeeMeter.model.EditMeterDto;
import com.orangelife.service.propFeeMeter.model.PropFeeMeterDto;
import com.orangelife.service.propFeeMeterRecord.IPropFeeMeterRecordService;
import com.orangelife.service.propFeeMeterRecord.model.PropFeeMeterRecordDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeMeter类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-07-04 11:04:21
 * ==============================================================
 */
@Service("propFeeMeterService")
public class PropFeeMeterServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeMeterService {
	
	@Resource(name = "feeLogService")
	private IFeeLogService feeLogService;
	@Resource(name="propFeeMeterRecordService")
	IPropFeeMeterRecordService propFeeMeterRecordService;
	
	private static Logger logger = Logger
			.getLogger(PropFeeMeterServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeMeterServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeMeter(PropFeeMeterDto propFeeMeter)
			throws AppBizException {

		Assert.notNull(propFeeMeter);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeMeter.getMeterName())) {
			if (propFeeMeter.getMeterName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "表名长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeter.getBelongType())) {
			if (propFeeMeter.getBelongType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"归属类型(0,小区,1楼栋,2单元，3房屋ID)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeter.getType())) {
			if (propFeeMeter.getType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"类型（0，公摊，1，用户）长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeter.getAttrType())) {
			if (propFeeMeter.getAttrType().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "属性类型长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeter.getDescription())) {
			if (propFeeMeter.getDescription().length() > 126) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "描述长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeter.getCode())) {
			if (propFeeMeter.getCode().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "code长度超出");
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("belongID", propFeeMeter.getBelongID());
		map.put("commID", propFeeMeter.getCommID());
		map.put("code", propFeeMeter.getCode());
		map.put("meterName", propFeeMeter.getMeterName());

		List<Map<String, Object>> propFeeTypeDtos = this
				.bauthfindPropFeeMeterByMap(map);
		if (propFeeTypeDtos != null && propFeeTypeDtos.size() > 0) {

			throw new AppBizException(2, "亲，该信息已经存在");
		}
		if (propFeeMeter.getMultiple() == null) {
			propFeeMeter.setMultiple(new BigDecimal(1));
		}
		propFeeMeter.setMeterID(IdUtil.getuuId());
		propFeeMeter.setItemStat(TypeConst.FLAG_ON);
		propFeeMeter.setTscreate(new Date());
		propFeeMeter.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeMeterMapper.insertSelective", propFeeMeter);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeMeter(PropFeeMeterDto propFeeMeter)
			throws AppBizException {

		Assert.notNull(propFeeMeter);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeMeter.getMeterID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeMeterDto oldPropFeeMeter = this
				.findPropFeeMeterByID(propFeeMeter.getMeterID());

		if (!StringUtils.isBlank(propFeeMeter.getMeterName())) {
			if (propFeeMeter.getMeterName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "表名长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeter.getBelongType())) {
			if (propFeeMeter.getBelongType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"归属类型(0,小区,1楼栋,2单元，3房屋ID)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeter.getType())) {
			if (propFeeMeter.getType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"类型（0，公摊，1，用户）长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeter.getAttrType())) {
			if (propFeeMeter.getAttrType().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "属性类型长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeter.getDescription())) {
			if (propFeeMeter.getDescription().length() > 126) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "描述长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeter.getCode())) {
			if (propFeeMeter.getCode().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "code长度超出");
			}
		}

		if (oldPropFeeMeter == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeMeter.setTsedit(new Date());

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("belongID", propFeeMeter.getBelongID());
		map.put("commID", propFeeMeter.getCommID());
		map.put("code", propFeeMeter.getCode());
		map.put("meterName", propFeeMeter.getMeterName());

		List<Map<String, Object>> propFeeTypeDtos = PropertyUtils
				.turnToEntity(this.bauthfindPropFeeMeterByMap(map));
		if (propFeeTypeDtos != null && propFeeTypeDtos.size() > 1) {

			throw new AppBizException(2, "亲，该信息已经存在");
		}

		if (propFeeTypeDtos != null && propFeeTypeDtos.size() == 1) {
			if (!propFeeTypeDtos.get(0).get("meterID").toString()
					.equals(propFeeMeter.getMeterID())) {
				throw new AppBizException(2, "亲，该信息已经存在");
			}
		}
		return this.getSqlSession().update(
				"PropFeeMeterMapper.updateByPrimaryKeySelective", propFeeMeter);
	}

	@Override
	public PropFeeMeterDto findPropFeeMeterByID(String propFeeMeterID)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeMeterID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeMeterDto oldPropFeeMeter = this.getSqlSession().selectOne(
				"PropFeeMeterSearchMapper.selectByPrimaryKey", propFeeMeterID);
		if (oldPropFeeMeter == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeMeter.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeMeter;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeMeterByLogic(String propFeeMeterID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeMeterID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeMeterDto oldPropFeeMeter = this
				.findPropFeeMeterByID(propFeeMeterID);

		oldPropFeeMeter.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeMeter.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeMeterMapper.updateByPrimaryKeySelective",
				oldPropFeeMeter);
	}

	@Override
	public List<Map<String, Object>> findPropFeeMeterByPage(String commID,
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);

		if (!StringUtils.isBlank(search)) {
			search = search.replaceAll("-", "");
		}
		map.put("commID", commID);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeMeterSearchMapper.findAllPropFeeMeter", map);

		/*
		 * if (list != null && list.size() > 0) { List<String> commIDs = new
		 * ArrayList<String>(); for (Map<String, Object> propFeeMeter : list) {
		 * commIDs.add(propFeeMeter.get("comm_id").toString()); }
		 * List<Map<String, Object>> comms = com.orangelife.utils.JsonMapUtils
		 * .getPageEntityMap(HttpSvcUtils.doPostSvc(UrlPath.bauthComm, commIDs,
		 * null));
		 * 
		 * Map<String, String> connValue = new HashMap<String, String>();
		 * 
		 * connValue.put("comm", "comm"); list = SplicingUtils.connoList(list,
		 * comms, "comm_id", "commID", connValue);
		 * 
		 * }
		 */
		return list;
	}

	@Override
	public List<PropFeeMeterDto> getPropFeeMeterByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeMeterDto> list = this.getSqlSession().selectList(
				"PropFeeMeterSearchMapper.getAllPropFeeMeterDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeMeterByRelevantID(
			String meterID, String commID, String meterTypeID, String belongID,
			String belongType, String type, String attrType)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("meterID", meterID);
		map.put("commID", commID);
		map.put("meterTypeID", meterTypeID);
		map.put("belongID", belongID);
		map.put("belongType", belongType);
		map.put("type", type);
		map.put("attrType", attrType);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeMeterSearchMapper.selectPropFeeMeterDtoyRelevantID",
				map);
		return list;
	}

	@Override
	public List<PropFeeMeterDto> findPropFeeMeterByRelevantID(String meterID,
			String commID, String meterTypeID, String belongID,
			String belongType, String type, String attrType)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("meterID", meterID);
		map.put("commID", commID);
		map.put("meterTypeID", meterTypeID);
		map.put("belongID", belongID);
		map.put("belongType", belongType);
		map.put("type", type);
		map.put("attrType", attrType);

		List<PropFeeMeterDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterSearchMapper.findPropFeeMeterDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeMeterDto> bauthfindPropFeeMeter(
			List<String> propFeeMeterIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeMeterDto> list = this.getSqlSession().selectList(
				"PropFeeMeterSearchMapper.batchFindPropFeeMeter",
				propFeeMeterIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeMeterByMap(
			Map<String, Object> propFeeMeter) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeMeterSearchMapper.batchFindPropFeeMeterByMap",
				propFeeMeter);
		return list;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int bauthAddPropFeeMeter(PropFeeMeterDto propFeeMeter)
			throws AppBizException {

		List<PropFeeMeterDto> propFeeMeterDtos = new ArrayList<PropFeeMeterDto>();

		if (StringUtils.isBlank(propFeeMeter.getType())) {
			throw new AppBizException(2, "亲  类型不可为空");
		}
		if (!propFeeMeter.getType().equals("0")
				&& !propFeeMeter.getType().equals("1")) {
			throw new AppBizException(2, "亲  类型不可为空");

		}
		/***
		 * 公摊
		 */
		if (propFeeMeter.getType().equals("0")) {
			if (StringUtils.isBlank(propFeeMeter.getBelongID())) {
				throw new AppBizException(2, "亲  归属类型不可为空");
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("belongID", propFeeMeter.getBelongID());
			map.put("commID", propFeeMeter.getCommID());
			map.put("code", propFeeMeter.getCode());
			map.put("meterName", propFeeMeter.getMeterName());
			List<Map<String, Object>> propFeeTypeDtos = this
					.bauthfindPropFeeMeterByMap(map);
			if (propFeeTypeDtos != null && propFeeTypeDtos.size() > 0) {
				throw new AppBizException(2, "亲，该信息已经存在");
			}

			if (propFeeMeter.getMultiple() == null) {
				propFeeMeter.setMultiple(new BigDecimal(1));
			}
			propFeeMeter.setBelongType("0");
			propFeeMeter.setCommID(propFeeMeter.getCommID());
			propFeeMeter.setCode(propFeeMeter.getCode());
			propFeeMeter.setMeterID(IdUtil.getuuId());
			propFeeMeter.setItemStat(TypeConst.FLAG_ON);
			propFeeMeter.setTscreate(new Date());
			propFeeMeter.setTsedit(new Date());
			propFeeMeterDtos.add(propFeeMeter);
		}

		/***
		 * 用户批量添加房屋
		 */
		if (propFeeMeter.getType().equals("1")) {
			if (propFeeMeter.getApartmentIDs() == null
					|| propFeeMeter.getApartmentIDs().size() <= 0) {
				throw new AppBizException(2, "亲  归属类型不可为空");
			}
			for (int i = 0; i < propFeeMeter.getApartmentIDs().size(); i++) {
				PropFeeMeterDto propFeeMeterDto = new PropFeeMeterDto();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("belongID", propFeeMeter.getApartmentIDs().get(i));
				map.put("commID", propFeeMeter.getCommID());
				map.put("code", propFeeMeter.getCode());
				map.put("meterName", propFeeMeter.getMeterName());
				List<Map<String, Object>> propFeeTypeDtos = this
						.bauthfindPropFeeMeterByMap(map);
				if (propFeeTypeDtos != null && propFeeTypeDtos.size() > 0) {
					throw new AppBizException(2, "亲，该信息已经存在");
				}
				propFeeMeterDto.setBelongID(propFeeMeter.getApartmentIDs().get(
						i));
				if (propFeeMeter.getMultiple() == null) {
					propFeeMeter.setMultiple(new BigDecimal(1));
				}
				propFeeMeterDto.setMultiple(propFeeMeter.getMultiple());
				propFeeMeterDto.setMeterName(propFeeMeter.getMeterName());
				propFeeMeterDto.setMeterTypeID(propFeeMeter.getMeterTypeID());
				propFeeMeterDto.setType(propFeeMeter.getType());
				propFeeMeterDto.setAttrType(propFeeMeter.getAttrType());
				propFeeMeterDto.setDescription(propFeeMeter.getDescription());
				propFeeMeterDto.setCommID(propFeeMeter.getCommID());
				propFeeMeterDto.setCode(propFeeMeter.getCode());
				propFeeMeterDto.setMeterID(IdUtil.getuuId());
				propFeeMeterDto.setItemStat(TypeConst.FLAG_ON);
				propFeeMeterDto.setTscreate(new Date());
				propFeeMeterDto.setTsedit(new Date());
				propFeeMeterDto.setBelongType("3");
				propFeeMeterDtos.add(propFeeMeterDto);

			}
		}
		return this.getSqlSession().insert(
				"PropFeeMeterBauthMapper.bauthInsert", propFeeMeterDtos);

	}

	@Override
	public int bauthDeletPropFeeMeter(List<String> propFeeMeterIDs)
			throws AppBizException {

		/****
		 * 批量删除
		 */
		return this.getSqlSession().update(
				"PropFeeMeterBauthMapper.bauthDeleteLg", propFeeMeterIDs);

	}

	@Override
	public List<Map<String, Object>> getNoRecordPropFeeMeter(
			Map<String, Object> map, String tsCreate, String search,
			int pageNo, int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/

		if (map == null) {
			map = new HashMap<String, Object>();
		}
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeMeterSearchMapper.getNoRecordPropFeeMeter", map);

		/*
		 * if (list != null && list.size() > 0) { List<String> commIDs = new
		 * ArrayList<String>(); List<String> belongIDs = new
		 * ArrayList<String>(); for (Map<String, Object> propFeeMeter : list) {
		 * commIDs.add(propFeeMeter.get("comm_id").toString());
		 * belongIDs.add(propFeeMeter.get("belong_id").toString()); }
		 * Map<String, String> connValue = new HashMap<String, String>();
		 * 
		 * if (commIDs.size() > 0) {
		 */
		/****
		 * 获取小区信息
		 */
		/*
		 * List<Map<String, Object>> comms = com.orangelife.utils.JsonMapUtils
		 * .getPageEntityMap(HttpSvcUtils.doPostSvc( UrlPath.bauthComm, commIDs,
		 * null)); connValue.put("comm", "comm");
		 * 
		 * list = SplicingUtils.connoList(list, comms, "comm_id", "commID",
		 * connValue); } if (belongIDs.size() > 0) { Map<String, Object> paMap =
		 * new HashMap<String, Object>(); paMap.put("apartmentIDs", belongIDs);
		 * List<Map<String, Object>> houserInfo =
		 * com.orangelife.utils.JsonMapUtils
		 * .getPageEntityMap(HttpSvcUtils.doPostSvc(
		 * UrlPath.bauthFindbuilngUntiApaprt, paMap, null));
		 * connValue.remove("comm"); connValue.put("houseName", "houseName");
		 * connValue.put("buildingName", "buildingName"); list =
		 * SplicingUtils.connoList(list, houserInfo, "belong_id", "apartmentID",
		 * connValue); }
		 * 
		 * }
		 */
		return list;
	}

	@Override
	public List<Map<String, Object>> getNoRecordPropFeeMeter(
			Map<String, Object> map, String tsCreate, String search)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/

		if (map == null) {
			map = new HashMap<String, Object>();
		}
		if (!StringUtils.isBlank(search)) {
			search = search.replaceAll("-", "");
		}
		map.put("tsCreate", tsCreate);
		map.put("search", search);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeMeterSearchMapper.getNoRecordPropFeeMeter", map);

		/*
		 * if (list != null && list.size() > 0) { List<String> commIDs = new
		 * ArrayList<String>(); List<String> belongIDs = new
		 * ArrayList<String>(); for (Map<String, Object> propFeeMeter : list) {
		 * commIDs.add(propFeeMeter.get("comm_id").toString());
		 * belongIDs.add(propFeeMeter.get("belong_id").toString()); }
		 * Map<String, String> connValue = new HashMap<String, String>();
		 * 
		 * if (commIDs.size() > 0) {
		 * 
		 * List<Map<String, Object>> comms = com.orangelife.utils.JsonMapUtils
		 * .getPageEntityMap(HttpSvcUtils.doPostSvc( UrlPath.bauthComm, commIDs,
		 * null)); connValue.put("comm", "comm");
		 * 
		 * list = SplicingUtils.connoList(list, comms, "comm_id", "commID",
		 * connValue); } if (belongIDs.size() > 0) { Map<String, Object> paMap =
		 * new HashMap<String, Object>(); paMap.put("apartmentIDs", belongIDs);
		 * List<Map<String, Object>> houserInfo =
		 * com.orangelife.utils.JsonMapUtils
		 * .getPageEntityMap(HttpSvcUtils.doPostSvc(
		 * UrlPath.bauthFindbuilngUntiApaprt, paMap, null));
		 * connValue.remove("comm"); connValue.put("houseName", "houseName");
		 * connValue.put("buildingName", "buildingName"); list =
		 * SplicingUtils.connoList(list, houserInfo, "belong_id", "apartmentID",
		 * connValue); }
		 * 
		 * }
		 */
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeMeterTreeByMap(
			Map<String, Object> propFeeMeter) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeMeterSearchMapper.batchFindPropFeeMeterTreeByMap",
				propFeeMeter);
		return list;
	}

	@Override
	public List<Map<String, Object>> findbuildUnitApartTree(String commID,
			String meterTypeID) throws AppBizException {
		Map<String, Object> propFeeMeter = new HashMap<String, Object>();

		propFeeMeter.put("commID", commID);
		propFeeMeter.put("meterTypeID", meterTypeID);
		List<Map<String, Object>> list = this.getSqlSession()
				.selectList("PropFeeMeterSearchMapper.findbuildUnitApartTree",
						propFeeMeter);
		return list;
	}

	@Override
	public List<Map<String, Object>> getInitRecordPropFeeMeter(
			Map<String, Object> map, String tsCreate, String search,
			int pageNo, int pageSize) throws AppBizException {

		if (map == null) {
			map = new HashMap<String, Object>();
		}
		if (!StringUtils.isBlank(search)) {
			search = search.replaceAll("-", "");
		}
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeMeterSearchMapper.getInitRecordPropFeeMeter", map);
		return list;
	}

	@Override
	public void dealMeterRecord(String commID, String date)
			throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("commID", commID);
		param.put("period", date);

		// 设置当期数据不完整的进行删除
		this.getSqlSession().update("PropFeeMeterMapper.delNorecord", param);

		// 设置当期无数据的取前期数据进行插入
		this.getSqlSession().insert("PropFeeMeterMapper.insertNorecord", param);

		// 把小区下的上期设置为往期
		this.updateMerRecordStatus(commID, null, "1", "2", null);

		// 把小区下的当前期数设置为上期，
		this.updateMerRecordStatus(commID, null, "0", "1", null);

	}

	@Override
	public int updateMerRecordStatus(String commID, String period,
			String bstatus, String astatus, String itemStat)
			throws AppBizException {

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("commID", commID);
		param.put("bstatus", bstatus);
		param.put("period", period);
		param.put("astatus", astatus);
		param.put("itemStat", itemStat);

		int n = this.getSqlSession().update("PropFeeMeterMapper.updateStatus",
				param);
		// TODO Auto-generated method stub
		return n;

	}

	@Override
	public List<Map<String, Object>> selectCommMeterRecord(Map<String, Object> param)
			throws AppBizException {
		
		
		
	
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeMeterSearchMapper.selectCommMeterRecord", param);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectMeterRecord(Map<String, Object> param)
			throws AppBizException {
		PageHelper.startPage(Integer.parseInt(param.get("pageIndex").toString()), Integer.parseInt(param.get("pageSize").toString()));
		
		if(param.get("type")==null||"1".equals(param.get("type").toString())){
			
			return this.getSqlSession().selectList(
					"PropFeeMeterSearchMapper.selectUserMeterRecord", param);
			
		}
		
		return this.getSqlSession().selectList(
				"PropFeeMeterSearchMapper.selectCommMeterRecord", param);
	
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editMeterRecords(EditMeterDto editMeterDto)
			throws AppBizException {
		
		//
		StringBuffer kkk=new StringBuffer();
		String lastDate="";
		PropFeeMeterRecordDto lastRecord=null;
		for (PropFeeMeterRecordDto propFeeMeterRecordDto : editMeterDto.getPropFeeMeterRecordDtos()) {
	if( StringUtils.isBlank(propFeeMeterRecordDto.getUseAmount())){
		propFeeMeterRecordDto.setUseAmount(null);
		
	}
			if(!StringUtils.isBlank(propFeeMeterRecordDto.getRecordID())){
				
				
				
				propFeeMeterRecordService.editPropFeeMeterRecordFull(propFeeMeterRecordDto);
				 kkk.append(propFeeMeterRecordDto.getMeterID()+","+propFeeMeterRecordDto.getPeriod()+"修改记录:读数  "+lastRecord.getRecord()+"改为"+propFeeMeterRecordDto.getRecord()+"使用量  "+lastRecord.getUseAmount()+"改为"+propFeeMeterRecordDto.getUseAmount());
	
			}else{
				
				List<PropFeeMeterRecordDto> l=	propFeeMeterRecordService.findPropFeeMeterRecordByRelevantID(null, propFeeMeterRecordDto.getMeterID(), null, null, propFeeMeterRecordDto.getPeriod(), null, null);

				if(l!=null&&l.size()>0){
					lastRecord=l.get(0);	
					propFeeMeterRecordDto.setRecordID(lastRecord.getRecordID());
					propFeeMeterRecordService.editPropFeeMeterRecordFull(propFeeMeterRecordDto);
					
					 kkk.append(propFeeMeterRecordDto.getMeterID()+","+propFeeMeterRecordDto.getPeriod()+"修改记录:读数  "+lastRecord.getRecord()+"改为"+propFeeMeterRecordDto.getRecord()+"使用量  "+lastRecord.getUseAmount()+"改为"+propFeeMeterRecordDto.getUseAmount());

				}else{
					 if(StringUtils.isBlank(propFeeMeterRecordDto.getPeriod())){
							throw new AppBizException(2, "期数信息不能为空");	  
						 
						 
					 }
					 if(StringUtils.isBlank(propFeeMeterRecordDto.getMeterID())){
							throw new AppBizException(2, "表信息不能为空");	  
						 
						 
					 }
					 
					 if(!editMeterDto.getInitDate().equals(propFeeMeterRecordDto.getPeriod())){
						 propFeeMeterRecordDto.setVer("1");
						 
					 }
					 
					propFeeMeterRecordService.addPropFeeMeterRecord(propFeeMeterRecordDto);
					 kkk.append(propFeeMeterRecordDto.getMeterID()+","+propFeeMeterRecordDto.getPeriod()+"新增记录:读数  "+propFeeMeterRecordDto.getRecord()+"使用量  "+propFeeMeterRecordDto.getUseAmount());
				}
				
				
			}
			

	
			
		}
		 feeLogService.addLog(editMeterDto.getCommID(), PropFeeLogDto.TYPE_USERMETER, PropFeeLogDto.OP_USERMETER_UPDATE, editMeterDto.getUserID(),editMeterDto.getTrueName()+"变更用户表读数",kkk.toString());

		return 0;
	}
	
	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editMeterRecordAndMoney(EditMeterDto editMeterDto)
			throws AppBizException {
		
		//
		StringBuffer kkk=new StringBuffer();
		String lastDate="";
		PropFeeMeterRecordDto lastRecord=null;
		for (PropFeeMeterRecordDto propFeeMeterRecordDto : editMeterDto.getPropFeeMeterRecordDtos()) {
	if( StringUtils.isBlank(propFeeMeterRecordDto.getUseAmount())){
		propFeeMeterRecordDto.setUseAmount(null);
		
	}
			if(!StringUtils.isBlank(propFeeMeterRecordDto.getRecordID())){
				
				
				
				propFeeMeterRecordService.editPropFeeMeterRecordAndMoney(propFeeMeterRecordDto);
				 kkk.append(propFeeMeterRecordDto.getMeterID()+","+propFeeMeterRecordDto.getPeriod()+"修改记录:读数  "+lastRecord.getRecord()+"改为"+propFeeMeterRecordDto.getRecord()+"金额  "+lastRecord.getFeeAmount()+"改为"+propFeeMeterRecordDto.getFeeAmount());
	
			}else{
				
				List<PropFeeMeterRecordDto> l=	propFeeMeterRecordService.findPropFeeMeterRecordByRelevantID(null, propFeeMeterRecordDto.getMeterID(), null, null, propFeeMeterRecordDto.getPeriod(), null, null);

				if(l!=null&&l.size()>0){
					lastRecord=l.get(0);	
					propFeeMeterRecordDto.setRecordID(lastRecord.getRecordID());
					propFeeMeterRecordService.editPropFeeMeterRecordAndMoney(propFeeMeterRecordDto);
					
					 kkk.append(propFeeMeterRecordDto.getMeterID()+","+propFeeMeterRecordDto.getPeriod()+"修改记录:读数  "+lastRecord.getRecord()+"改为"+propFeeMeterRecordDto.getRecord()+"金额  "+lastRecord.getFeeAmount()+"改为"+propFeeMeterRecordDto.getFeeAmount());

				}else{
					 if(StringUtils.isBlank(propFeeMeterRecordDto.getPeriod())){
							throw new AppBizException(2, "期数信息不能为空");	  
						 
						 
					 }
					 if(StringUtils.isBlank(propFeeMeterRecordDto.getMeterID())){
							throw new AppBizException(2, "表信息不能为空");	  
						 
						 
					 }
					 
					 if(!editMeterDto.getInitDate().equals(propFeeMeterRecordDto.getPeriod())){
						 propFeeMeterRecordDto.setVer("1");
						 
					 }
					 
					propFeeMeterRecordService.addPropFeeMeterRecord(propFeeMeterRecordDto);
					 kkk.append(propFeeMeterRecordDto.getMeterID()+","+propFeeMeterRecordDto.getPeriod()+"新增记录:读数  "+propFeeMeterRecordDto.getRecord()+"使用量  "+propFeeMeterRecordDto.getUseAmount());
				}
				
				
			}
			

	
			
		}
		 feeLogService.addLog(editMeterDto.getCommID(), PropFeeLogDto.TYPE_USERMETER, PropFeeLogDto.OP_USERMETER_UPDATE, editMeterDto.getUserID(),editMeterDto.getTrueName()+"变更用户表读数",kkk.toString());

		return 0;
	}
}
